package com.jhd.fuzhou.util;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.IBinder;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.jhd.fuzhou.view.login.ForgetPasswordActivity;
import com.jhd.fuzhou.view.login.FuzhouApplication;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.vo.QuestionBankOptionVo;
import com.jhd.fuzhou.vo.SupervisionInspectionContentVo;

import java.util.List;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.fastjson.FastJsonConverterFactory;

public class UtilHelper {
    public static int ePrinterType=1;//打印机牌子，0为佳能，1为爱普生

    public static class baseUrls {
        public static String API() {
//            return "http://106.15.46.65:9000/api/";//开发环境,已作废
//            return "http://192.168.1.163:9004/api/";//陈吉本地
//            return "http://1.95.11.127:8080/api/";//开发环境
            return "http://117.21.49.57:9010/api/";//生产
//            return "http://tech.lhtsky.top:42312/api/";//王超本地
//            return "http://118.190.103.237:9000/api/";//测试环境
//            return "http://gaoqiang.natapp1.cc/api/";//高强本地
        }
        public static String UC() {
            return API()+"uc/";
        }
        //版本更新
//        public static String VERSION = "http://121.42.143.30:9527/";
        public static String VERSION = "http://112.25.248.254:38080/";
//        public static String ERR_TRACE = "http://121.42.143.30:9527/";

        //闪退等异常追踪信息上传地址
        public static String ERR_TRACE = "http://112.25.248.254:38080/";
    }
    public static void hideKeyboard(MotionEvent event, View view,
                                    Activity activity) {
        try {
            if (view != null && view instanceof EditText) {
                int[] location = {0, 0};
                view.getLocationInWindow(location);
                int left = location[0], top = location[1], right = left
                        + view.getWidth(), bootom = top + view.getHeight();
                // 判断焦点位置坐标是否在空间内，如果位置在控件外，则隐藏键盘
                if (event.getRawX() < left || event.getRawX() > right
                        || event.getY() < top || event.getRawY() > bootom) {
                    // 隐藏键盘
                    IBinder token = view.getWindowToken();
                    InputMethodManager inputMethodManager = (InputMethodManager) activity
                            .getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.hideSoftInputFromWindow(token,
                            InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void alertMsg(Context context, String title, String msg) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setIcon(R.mipmap.dialog_alert);
        builder.setMessage(msg);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        Dialog dialog = builder.create();
        if (!((Activity) context).isFinishing()) dialog.show();

        int color = context.getResources().getColor(R.color.header);
        Button btn = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
        if (btn != null) btn.setTextColor(color);
    }
    public static void alertMsg2(Context context, String title, String msg) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setIcon(R.mipmap.dialog_alert);
        builder.setMessage(msg);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                ((Activity)context).finish();
            }
        });
        Dialog dialog = builder.create();
        if (!((Activity) context).isFinishing()) dialog.show();

        int color = context.getResources().getColor(R.color.header);
        Button btn = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
        if (btn != null) btn.setTextColor(color);
    }
    public static void alertMsg3(Context context, String title, String msg) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setIcon(R.mipmap.dialog_alert);
        builder.setMessage(msg);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                ((Activity)context).startActivity(new Intent(context, ForgetPasswordActivity.class));
            }
        });
        Dialog dialog = builder.create();
        if (!((Activity) context).isFinishing()) dialog.show();

        int color = context.getResources().getColor(R.color.header);
        Button btn = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
        if (btn != null) btn.setTextColor(color);
    }

    @FunctionalInterface
    public interface SelectOK {
        void execute();
    }

    private static void handleOK(SelectOK selectOK) {
        selectOK.execute();
    }

    public static void dialogSelector(Context context, String title, String msg, final SelectOK toDo) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setIcon(R.mipmap.dialog_alert);
        builder.setMessage(msg);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                handleOK(toDo);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        if (!((Activity) context).isFinishing()) builder.show();
    }

    public static class Cache {
        public static String USER = "user";
    }

    public static String DOWNLOAD_RECEIVER = "com.jhd.fuzhou.download.RECEIVER";


    public static Retrofit retrofit(String baseUrl) {
        OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
        return new Retrofit.Builder().baseUrl(baseUrl).addConverterFactory(FastJsonConverterFactory.create()).client(builder.build()).build();
    }

    public static Retrofit retrofit(FuzhouApplication application, String baseUrl) {
        OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
        builder.addInterceptor(new NetInterceptor(application));
        return new Retrofit.Builder().baseUrl(baseUrl).addConverterFactory(FastJsonConverterFactory.create()).client(builder.build()).build();
    }

}
